package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.operationmanual;

import android.app.Activity;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.CouponListBean;
import com.zzsy.carosshopprojects.bean.MemberInfoBean;
import com.zzsy.carosshopprojects.bean.SpaceCourseDataBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import androidx.lifecycle.MutableLiveData;

public class OperationManualMode extends BaseViewMode {
    private  String memberId="";
    private MutableLiveData<ArrayList<SpaceCourseDataBean>> live;
    private MutableLiveData<Integer> liveCode;
    public MutableLiveData<ArrayList<SpaceCourseDataBean>> getLiveData() {
        if (live == null)
            live = new MutableLiveData<>();
        return live;
    }
    public MutableLiveData<Integer> getLiveCodeData() {
        if (liveCode == null)
            liveCode = new MutableLiveData<>();
        return liveCode;
    }
    /**
     * 根据UUID查询会员信息
     *
     * @param context
     * @param uuid
     * @param
     */
    public void MemberByUuid(Activity context, String uuid) {
        RequestUtils.MemberByUuid(context, uuid, DataUtil.getInstance().getCompanyId(), new MyObserver<MemberInfoBean>(context) {
            @Override
            public void onSuccess(MemberInfoBean bean) {
                 memberId = bean.getMemberId();
                if (StringUtils.isEmpty(memberId)) {
                    ToastUtils.showToast(context.getApplicationContext(), "该会员异常！");
                    context.finish();
                } else {
                    reqAllCourseList(context, memberId);
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }

    /**
     * 获取该会员下的费油优惠券
     */
    private void reqAllCourseList(Activity activity, String memberId) {
        RequestUtils.GetMemberCouponList(activity, memberId, new MyObserver<CouponListBean>(activity) {
            @Override
            public void onSuccess(CouponListBean result) {
                ArrayList<SpaceCourseDataBean> beanList = new ArrayList<>();
                if (result != null && result.getList().size() != 0) {

                    for (int i = 0; i < result.getList().size(); i++) {
                        SpaceCourseDataBean spaceCourseData = new SpaceCourseDataBean();
                        spaceCourseData.setCouponName(result.getList().get(i).getCouponName());
                        spaceCourseData.setEndTime(result.getList().get(i).getTimeOut());
                        spaceCourseData.setFullAmount(result.getList().get(i).getMinimumAmount());
                        spaceCourseData.setId(result.getList().get(i).getId());
                        spaceCourseData.setPreAmount(result.getList().get(i).getPreAmount());
                        spaceCourseData.setPreType(result.getList().get(i).getPreType());
                        spaceCourseData.setStartTime(result.getList().get(i).getStartTimeStr());
                        spaceCourseData.setPreKind(result.getList().get(i).getPreKind());
                        spaceCourseData.setRebate(result.getList().get(i).getRebate());
                        beanList.add(spaceCourseData);
                    }
                    live.postValue(beanList);
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(activity.getApplicationContext(), errorMsg);
            }
        });
    }

    public void getToken(Activity context, String couponIds) {
        getToken(context, token -> {
            hexiaoData(context, couponIds,token);
        });
    }

    /**
     * 调取核销接口
     */
    public void hexiaoData(Activity activity, String couponIds,String token) {
        Map<String, String> map = new HashMap<>();
        map.put("couponIds", couponIds);
        map.put("memberId", memberId);
        map.put("terminalId", DataUtil.getInstance().getTerminalId());
        map.put("usedStoreId", DataUtil.getInstance().getStoreId());
        RequestUtils.UseNonOilCoupon(activity, map, token, new MyObserver<BaseBean>(activity) {
            @Override
            public void onSuccess(BaseBean result) {
                liveCode.postValue(200);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(activity.getApplicationContext(), errorMsg);
            }
        });
    }
}
